package com.example.seabedmusic.views;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CompoundButton;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.seabedmusic.R;
import com.example.seabedmusic.adapter.NotificationSettingsAdapter;
import com.example.seabedmusic.entity.NotificationSettingItem;
import java.util.ArrayList;
import java.util.List;

public class NotificationSettingsActivity extends AppCompatActivity implements NotificationSettingsAdapter.OnSwitchChangeListener {

    private RecyclerView rvNotifications;
    private NotificationSettingsAdapter adapter;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings_list);

        preferences = getSharedPreferences("notification_settings", MODE_PRIVATE);
        initViews();
        setupNotificationList();
    }

    private void initViews() {
        rvNotifications = findViewById(R.id.rvSettings);
        rvNotifications.setLayoutManager(new LinearLayoutManager(this));
        setTitle("通知设置");
    }

    private void setupNotificationList() {
        List<NotificationSettingItem> items = new ArrayList<>();

        items.add(new NotificationSettingItem("推送通知", "接收应用推送消息",
                preferences.getBoolean("push_notifications", true)));
        items.add(new NotificationSettingItem("新歌提醒", "关注歌手发布新歌时通知",
                preferences.getBoolean("new_song_notifications", true)));
        items.add(new NotificationSettingItem("私信通知", "收到私信时提醒",
                preferences.getBoolean("message_notifications", true)));
        items.add(new NotificationSettingItem("评论回复", "收到评论回复时通知",
                preferences.getBoolean("comment_notifications", true)));
        items.add(new NotificationSettingItem("系统通知", "系统维护、更新等通知",
                preferences.getBoolean("system_notifications", true)));
        items.add(new NotificationSettingItem("夜间免打扰", "22:00-8:00不发送通知",
                preferences.getBoolean("night_mode", false)));

        adapter = new NotificationSettingsAdapter(items);
        adapter.setOnSwitchChangeListener(this);
        rvNotifications.setAdapter(adapter);
    }

    @Override
    public void onSwitchChanged(int position, boolean isChecked) {
        NotificationSettingItem item = adapter.getItemAtPosition(position);
        if (item != null) {
            preferences.edit().putBoolean(getPreferenceKey(item.getTitle()), isChecked).apply();
        }
    }

    private String getPreferenceKey(String title) {
        switch (title) {
            case "推送通知": return "push_notifications";
            case "新歌提醒": return "new_song_notifications";
            case "私信通知": return "message_notifications";
            case "评论回复": return "comment_notifications";
            case "系统通知": return "system_notifications";
            case "夜间免打扰": return "night_mode";
            default: return title;
        }
    }
}